/**HEADER********************************************************************
* 
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
* Copyright (c) 2004-2010 Embedded Access Inc.;
* All Rights Reserved
*
* Copyright (c) 1989-2008 ARC International;
* All Rights Reserved
*
*************************************************************************** 
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
* THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************
*
* $FileName: mpc8308_etsec.c$
* $Version : 3.8.1.0$
* $Date    : May-12-2011$
*
* Comments:
*
*   This file initializes the 83xx ethernet interface.
*
*END************************************************************************/

#include <mqx.h>
#include <bsp.h>
#include <bsp_prv.h>
#include <ethernet.h>
#include <enet.h>
#include <enetprv.h>
#include <mdio.h>
#include <mpc83xx_etsec_prv.h>


const uint_32 MPC83xx_etsec_vectors[MPC83xx_ETSEC_DEVICE_COUNT][MPC83xx_ETSEC_NUM_INTS] = 
{
    { MPC83xx_TSEC1_TX_VECTOR, MPC83xx_TSEC1_RX_VECTOR, MPC83xx_TSEC1_ERR_VECTOR }, 
    { MPC83xx_TSEC2_TX_VECTOR, MPC83xx_TSEC2_RX_VECTOR, MPC83xx_TSEC2_ERR_VECTOR }
};

/*FUNCTION*-------------------------------------------------------------------
* 
* Function Name    : MPC83xx_etsec_get_base_address  
* Returned Value   : Pointer to desired enet device or NULL if not present
* Comments         :
*    This function returns pointer to base address of address space of the 
*    desired enet device. Returns NULL otherwise.
*
*END*----------------------------------------------------------------------*/

MPC83xx_ETSEC_STRUCT_PTR MPC83xx_etsec_get_base_address( uint_32 device )
{ 
   MPC83xx_ETSEC_STRUCT_PTR tsec_ptr = NULL;
   uint_32                  immr;

  _PSP_GET_IMMR(immr);

   if (device==0) {
      tsec_ptr = (MPC83xx_ETSEC_STRUCT_PTR)(immr + MPC83xx_TSEC1_BASE);
   } else if (device==1) {
      tsec_ptr = (MPC83xx_ETSEC_STRUCT_PTR)(immr + MPC83xx_TSEC2_BASE);
   } else {
      tsec_ptr = NULL;
   }
   
   return tsec_ptr;
}


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_pin_config
*  Returned Value : None
*  Comments       :
*        Configures TSEC unit multiplexed pins
*
*END*-----------------------------------------------------------------*/

void MPC83xx_etsec_pin_config( uint_32 device )
{ 
   MPC83xx_SYSCON_STRUCT_PTR  syscon_ptr;
   uint_32                    immr;

  _PSP_GET_IMMR(immr);
   
   syscon_ptr = (MPC83xx_SYSCON_STRUCT_PTR)(immr + MPC83xx_SYSCON_BASE);

   if(device == 0) {
      /* TSEC1 */
      // Nothing to do   
   } else {
      /* TSEC2 */   
      syscon_ptr->SICRH &= ~(MPC8308_SYSCON_SICRH_GPIO_A(3) | MPC8308_SYSCON_SICRH_ETSEC2(3));
      syscon_ptr->SICRH |= MPC8308_SYSCON_SICRH_GPIO_A(1) | MPC8308_SYSCON_SICRH_ETSEC2(1);
   } 
} 


/*FUNCTION*-------------------------------------------------------------
*
*  Function Name  : MPC83xx_etsec_clock_config
*  Returned Value : None
*  Comments       :
*        Configures TSEC unit multiplexed pins
*
*END*-----------------------------------------------------------------*/

void MPC83xx_etsec_clock_config( uint_32 device )
{
   MPC83xx_CLKMOD_STRUCT_PTR  clkmod_ptr;
   uint_32                    immr;

  _PSP_GET_IMMR(immr);

   // Setup clocks and tsec ptrs
   clkmod_ptr = (MPC83xx_CLKMOD_STRUCT_PTR)(immr + MPC83xx_CLOCK_MODULE_BASE);
   
   if(device == 0) {
      /* Setup TSEC1 clock to csbclk */
      clkmod_ptr->SCCR &= ~MPC83xx_CLKMOD_SCCR_TSEC1CM_MASK;     
      clkmod_ptr->SCCR |= MPC83xx_CLKMOD_SCCR_TSEC1CM_DIV1;
   } else {
      /* Setup TSEC2 clock to csbclk */
      clkmod_ptr->SCCR &= ~MPC83xx_CLKMOD_SCCR_TSEC2CM_MASK;     
      clkmod_ptr->SCCR |= MPC83xx_CLKMOD_SCCR_TSEC2CM_DIV1;
   } 
}
